package com.oil.petro.eshop.security;

import com.oil.petro.eshop.entity.Account;
import com.oil.petro.eshop.service.UserAccountService;
import com.oil.petro.eshop.service.UserAccountServiceImpl;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Service
public class AuthenticationProvider implements UserDetailsService {

    @Autowired
    UserAccountService accountService;

    @Override
    public UserDetails loadUserByUsername(String login) {
System.out.println("Autoryzacja2");
        //UserAccountService accountService = new UserAccountServiceImpl();
        Account account = accountService.getAccountByLogin(login);

        if (account == null) {
            System.out.println("Autoryzacjaaaaaa");
           throw new UsernameNotFoundException("Account for login " + login + "was not found.");
        }
        System.out.println("Autoryzacja3");
        System.out.println(account.getLogin()+" f "+ account.getPassword()+" f "+account.getRole());
        String authorities = account.getRole();
        Set<SimpleGrantedAuthority> auth = new HashSet<>();
        auth.add(new SimpleGrantedAuthority(authorities));
      //  auth.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
      //  auth.add(new SimpleGrantedAuthority("ROLE_USER"));
        return new User(account.getLogin(), account.getPassword(), true, true, true, true, auth);
    }
    
    
}
